package de.freudenreichdesign.webshop.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table (name="PURCHASE_ORDER")
public class Order implements Serializable {
	
	private static final long serialVersionUID = 2666864782113096402L;
	private long orderID;
	private Set<ShoppingCartItem> articles = new HashSet<ShoppingCartItem>();
	private Adress deliveryAdress;
	private Adress invoiceAdress;
	private Customer customer;
	
	@SuppressWarnings("unused")
	private Order() {}
	
	public Order(Set<ShoppingCartItem> articles, Adress deliveryAdress, Adress invoiceAdress, Customer customer) {
		this.articles = articles;
		this.deliveryAdress = deliveryAdress;
		this.invoiceAdress = invoiceAdress;
		this.customer = customer;
	}
	
	@Id
	@GeneratedValue (strategy=GenerationType.AUTO)
	@Column (name="ORDER_ID")
	public long getOrderID() {
		return orderID;
	}
	
	@SuppressWarnings("unused")
	private void setOrderID(long orderID) {
		this.orderID = orderID;
	}
	
	@OneToMany (cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinTable(name="ITEMS_OF_ORDER",
            joinColumns = @JoinColumn(name="ORDER_ID"),
            inverseJoinColumns = @JoinColumn(name="ITEM_ID"))
	public Set<ShoppingCartItem> getArticle() {
		return articles;
	}
	
	public void setArticle(Set<ShoppingCartItem> articles) {
		this.articles = articles;
	}
	
	@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinColumn(name="DELIVERY_ADRESS_ID")
	public Adress getDeliveryAdress() {
		return deliveryAdress;
	}
	
	public void setDeliveryAdress(Adress deliveryAdress) {
		this.deliveryAdress = deliveryAdress;
	}
	
	@ManyToOne (cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinColumn(name="INVOICE_ADRESS_ID")
	public Adress getInvoiceAdress() {
		return invoiceAdress;
	}
	
	public void setInvoiceAdress(Adress invoiceAdress) {
		this.invoiceAdress = invoiceAdress;
	}
	
	@ManyToOne (cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinColumn(name="CUSTOMER_ID")
	public Customer getCustomer() {
		return customer;
	}
	
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
}
